<template>
  <view>
    <view class="goods-item" @click="itemClickHandler">
      <view class="goods-radio">
        <!-- 选中框 -->
        <radio :checked="goods.goods_state" color="#c00000" v-if="showRadio" @click="radioChangeHandler"></radio>
        <!-- 商品左侧图片区域 -->
      </view>
      <view class="goods-item-left">
        <image class="goods-image" :src="goods.goods_small_logo || defaultPic"></image>
      </view>
      <!-- 商品右侧信息区域 -->
      <view class="goods-item-right">
        <!-- 商品标题 -->
        <view class="goods-name">{{goods.goods_name}}</view>
        <view class="goods-info-box">
          <!-- 商品价格 -->
          <view class="goods-price">￥{{Number(goods.goods_price).toFixed(2)}}</view>
          <!-- 商品数量 -->
          <uni-number-box :min="1" :value="goods.goods_count" @change="numChangeHandler" v-if="showNumBox"></uni-number-box>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "my_goods",
    data() {
      return {
        // 默认图片
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
      };
    },

    props: {
      goods: {
        type: Object,
        default: {}
      },


      // 选中框的显示与隐藏
      showRadio: {
        type: Boolean,
        default: false
      },

      // 数量框显示/隐藏
      showNumBox: {
        type: Boolean,
        default: false
      }
    },

    methods: {
      itemClickHandler() {
        this.$emit('click', this.goods)
      },

      // 点击选中框
      radioChangeHandler() {
        this.$emit('radio-change', {
          goods_id: this.goods.goods_id,
          goods_state: this.goods.goods_state
        })
      },
      
      // 数量框改变
      numChangeHandler(value) {
        this.$emit('num-change', {
          goods_id: this.goods.goods_id,
          goods_count: value
        })
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    width: 750rpx;
      // 设置盒模型为 border-box
      box-sizing: border-box;
      display: flex;
      padding: 10px 5px;
      border-bottom: 1px solid #f0f0f0;

    .goods-radio {
      display: flex;
      align-items: center;
    }
    .goods-item-left {
      .goods-image {
        width: 200rpx;
        height: 200rpx;
        vertical-align: middle;
      }
    }

    .goods-item-right {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .goods-name {
        font-size: 24rpx;
      }

      .goods-info-box {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }

      .goods-price {
        font-size: 32rpx;
        color: #c00000;
      }
    }
  }
</style>
